package cn.stylefeng.guns.modular.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.BusinessException;
import cn.stylefeng.guns.modular.business.entity.WxPay;
import cn.stylefeng.guns.modular.business.exception.WxPayExceptionEnum;
import cn.stylefeng.guns.modular.business.mapper.WxPayMapper;
import cn.stylefeng.guns.modular.business.pojo.WxPayRequest;
import cn.stylefeng.guns.modular.business.service.WxPayService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 车辆管理业务实现层
 *
 * @author 施律名
 * @date 2022-03-10 17:48:59
 */
@Service
public class WxPayServicelmpl extends ServiceImpl<WxPayMapper, WxPay> implements WxPayService {

    @Override
    public void add(WxPayRequest wxPayRequest) {
        WxPay wxPay = new WxPay();
        BeanUtil.copyProperties(wxPayRequest, wxPay);
        this.save(wxPay);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(WxPayRequest wxPayRequest) {
        WxPay wxPay = this.queryWxPay(wxPayRequest);
        this.removeById(wxPay.getWxPayId());
    }

    @Override
    public void edit(WxPayRequest wxPayRequest) {
        WxPay wxPay = this.queryWxPay(wxPayRequest);
        BeanUtil.copyProperties(wxPayRequest, wxPay);
        this.updateById(wxPay);
    }

    @Override
    public WxPay detail(WxPayRequest wxPayRequest) {
        return this.queryWxPay(wxPayRequest);
    }

    @Override
    public PageResult<WxPay> findPage(WxPayRequest wxPayRequest) {
        LambdaQueryWrapper<WxPay> wrapper = createWrapper(wxPayRequest);
        Page<WxPay> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<WxPay> findList(WxPayRequest wxPayRequest) {
        LambdaQueryWrapper<WxPay> wrapper = this.createWrapper(wxPayRequest);
        return this.list(wrapper);
    }

    /**
     * 获取车辆信息
     *
     * @author 施律名
     * @date 2022-03-10 17:48:59
     */
    private WxPay queryWxPay(WxPayRequest wxPayRequest) {
        WxPay wxPay = this.getById(wxPayRequest.getWxPayId());
        if (ObjectUtil.isEmpty(wxPay)) {
            throw new BusinessException(WxPayExceptionEnum.WX_PAY_NOT_EXISTED);
        }
        return wxPay;
    }

    /**
     * 创建查询wrapper
     *
     * @author 施律名
     * @date 2022-03-10 17:48:59
     */
    private LambdaQueryWrapper<WxPay> createWrapper(WxPayRequest wxPayRequest) {
        LambdaQueryWrapper<WxPay> queryWrapper = new LambdaQueryWrapper<>();

        Long wxPayId = wxPayRequest.getWxPayId();
        queryWrapper.like(ObjectUtil.isNotEmpty(wxPayId), WxPay::getWxPayId, wxPayId);

        // 根据时间倒序排列
        queryWrapper.orderByDesc(WxPay::getCreateTime);

        return queryWrapper;
    }


}